package com.hu.zhcc.shiro.controller.dingming;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.dingming.TMeals;
import com.hu.zhcc.shiro.service.dingming.TMealsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/meals")
public class TMealsController {
    @Autowired
    private TMealsService tMealsService;

    @GetMapping("")
    public ResponseEntity<Page<TMeals>> getListMeals(@RequestParam(required = false) String name,int offset, int limit) {
        Map<String, Object> paremeters = new HashMap<String, Object>();
        paremeters.put("name", name);
        Page<TMeals> result = tMealsService.listTMeals(paremeters, offset, limit);
        System.out.println(result.getRows().get(0).getTMealsType());
        return ResponseEntity.ok(result);
    }

    @PostMapping("")
    public ResponseEntity<TMeals> saveCheckItem(@RequestBody TMeals checkItem) {
        checkItem.setCreateTime(new Date());
        int insert = tMealsService.insert(checkItem);
        return ResponseEntity.status(HttpStatus.CREATED).body(checkItem);
    }

    @GetMapping("/{id}")
    public ResponseEntity<TMeals> getCheckItemById(@PathVariable("id") Integer id) {
        TMeals checkItem = tMealsService.selectByPrimaryKey(id);
        if (checkItem != null) {
            return ResponseEntity.ok(checkItem);
        }
        return ResponseEntity.notFound().build();
    }

    @PutMapping("")
    public ResponseEntity<Integer> updateCheckItem(@RequestBody TMeals checkItem) {
        int rows = tMealsService.updateByPrimaryKey(checkItem);
        if (rows == 0) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.status(HttpStatus.CREATED).body(rows);
    }

}
